﻿// -----------------------------------------------------------------------
// <copyright file="SmallIntColumnDto.cs" company="JSC Centido">
//  Copyright (c) JSC Centido. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;

namespace Centido.Core.Dto
{
    /// <summary>
    ///   A data transfer object for a small integer table column.
    /// </summary>
    public class SmallIntColumnDto : ColumnDto
    {
        /// <summary>
        ///   The name of the xml tag that is used to store this object.
        /// </summary>
        public const string XmlTag = "smallIntColumn";

        /// <summary>
        ///   The current xml schema version.
        /// </summary>
        public const string CurrentXmlSchemaVersion = "1";

        /// <summary>
        ///   The name of the xml attribute that is used to store the column default value.
        /// </summary>
        public const string DefaultValueAttribute = "defaultValue";

        /// <summary>
        ///   The name of the xml attribute that is used to store the column maximum value.
        /// </summary>
        public const string MaximumAttribute = "maxValue";

        /// <summary>
        ///   The name of the xml attribute that is used to store the column minimum value.
        /// </summary>
        public const string MinimumAttribute = "minValue";


        /// <summary>
        ///   Gets the column type.
        /// </summary>
        public override ColumnType ColumnType
        {
            get { return ColumnType.SmallInteger; }
        }


        /// <summary>
        ///   Gets or sets the column default value.
        /// </summary>
        public short? DefaultValue { get; set; }


        /// <summary>
        ///   Gets or sets the column maximum value.
        /// </summary>
        public short? Maximum { get; set; }


        /// <summary>
        ///   Gets or sets the column minimum value.
        /// </summary>
        public short? Minimum { get; set; }


        /// <summary>
        ///   This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
        /// </returns>
        public override XmlSchema GetSchema()
        {
            return null;
        }


        /// <summary>
        ///   Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized. 
        /// </param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="reader"/> is null.</exception>
        public override void ReadXml(XmlReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            reader.MoveToContent();

            // Reading the xml schema version
            var xmlSchemaVersion = ReadStringAttribute(reader, XmlSchemaVersionTag, isRequired: false);

            // If the xml schema version is not defined
            if (string.IsNullOrEmpty(xmlSchemaVersion)
                // or if it is the current xml schema version
                || xmlSchemaVersion == CurrentXmlSchemaVersion)
            {
                // Reading the column name
                Name = ReadStringAttribute(reader, NameAttribute);

                // Reading the display name
                DisplayName = ReadStringAttribute(reader, DisplayNameAttribute, isRequired: false);

                // Reading the description
                Description = ReadStringAttribute(reader, DescriptionAttribute, isRequired: false);

                // Reading the order index
                OrderIndex = ReadIntAttribute(reader, OrderIndexAttribute, isRequired: false);

                // Reading the column nullability
                IsNotNull = ReadBoolAttribute(reader, IsNotNullAttribute, isRequired: false);

                // Reading the default value
                DefaultValue = ReadSmallIntAttribute(reader, DefaultValueAttribute, isRequired: false);

                // Reading the maximum value
                Maximum = ReadSmallIntAttribute(reader, MaximumAttribute, isRequired: false);

                // Reading the minimum value
                Minimum = ReadSmallIntAttribute(reader, MinimumAttribute, isRequired: false);

                var isEmptyElement = reader.IsEmptyElement;

                reader.ReadStartElement();

                if (!isEmptyElement)
                    reader.ReadEndElement();
            }
            else
                throw new ArgumentException(string.Format(
                    "The specified xml schema version is not supported (node = {0}, version = {1}).",
                    reader.Name,
                    xmlSchemaVersion));
        }


        /// <summary>
        ///   Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized. 
        /// </param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="writer"/> is null.</exception>
        public override void WriteXml(XmlWriter writer)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");

            writer.WriteStartElement(XmlTag);

            // The xml schema version
            writer.WriteAttributeString(XmlSchemaVersionTag, CurrentXmlSchemaVersion);

            // The column name
            if (!string.IsNullOrEmpty(Name))
                writer.WriteAttributeString(NameAttribute, Name);

            // The column display name
            if (!string.IsNullOrEmpty(DisplayName))
                writer.WriteAttributeString(DisplayNameAttribute, DisplayName);

            // The column description
            if (!string.IsNullOrEmpty(Description))
                writer.WriteAttributeString(DescriptionAttribute, Description);

            // The column order index
            if (OrderIndex.HasValue)
                writer.WriteAttributeString(OrderIndexAttribute, 
                    OrderIndex.Value.ToString(CultureInfo.InvariantCulture));

            // The column nullability
            if (IsNotNull.HasValue)
                writer.WriteAttributeString(IsNotNullAttribute, 
                    IsNotNull.Value.ToString(CultureInfo.InvariantCulture));

            // The column default value
            if (DefaultValue.HasValue)
                writer.WriteAttributeString(DefaultValueAttribute, 
                    DefaultValue.Value.ToString(CultureInfo.InvariantCulture));

            // The column maximum value
            if (Maximum.HasValue)
                writer.WriteAttributeString(MaximumAttribute, 
                    Maximum.Value.ToString(CultureInfo.InvariantCulture));

            // The column minimum value
            if (Minimum.HasValue)
                writer.WriteAttributeString(MinimumAttribute, 
                    Minimum.Value.ToString(CultureInfo.InvariantCulture));

            writer.WriteEndElement();
        }


        /// <summary>
        ///   Copies the properties of the data transfer object to the specified small integer column.
        /// </summary>
        /// <param name="column">The small integer column.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="column"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown when the column has the type other than small integer.</exception>
        public override void CopyTo(Column column)
        {
            if (column == null)
                throw new ArgumentNullException("column");
            if (column.ColumnType != ColumnType.SmallInteger)
                throw new ArgumentException("The column type should be small integer.");

            var smallIntColumn = (SmallIntColumn)column;

            // The column name
            if (!string.IsNullOrEmpty(Name) && Name != column.Name)
                column.Name = Name;

            // The column display name
            if (DisplayName != column.DisplayName)
                column.DisplayName = DisplayName;

            // The column description
            if (Description != column.Description)
                column.Description = Description;

            // The column order index
            if (OrderIndex.HasValue && OrderIndex != column.OrderIndex)
                column.OrderIndex = OrderIndex.Value;

            // The column nullability
            if (IsNotNull.HasValue && IsNotNull != column.IsNotNull)
                column.IsNotNull = IsNotNull.Value;

            // The column default value
            if (DefaultValue != smallIntColumn.DefaultValue)
                smallIntColumn.DefaultValue = DefaultValue;

            // The column maximum value
            if (Maximum != smallIntColumn.Maximum)
                smallIntColumn.Maximum = Maximum;

            // The column minimum value
            if (Minimum != smallIntColumn.Minimum)
                smallIntColumn.Minimum = Minimum;
        }


        /// <summary>
        ///   Creates a data transfer object for the specified small integer column.
        /// </summary>
        /// <param name="smallIntColumn">The specified small integer column.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="smallIntColumn"/> is null.</exception>
        /// <returns>Returns the data transfer object for the specified small integer column.</returns>
        public static SmallIntColumnDto CreateFrom(SmallIntColumn smallIntColumn)
        {
            if (smallIntColumn == null)
                throw new ArgumentNullException("smallIntColumn");

            return new SmallIntColumnDto
            {
                Name = smallIntColumn.Name,
                DisplayName = smallIntColumn.DisplayName,
                Description = smallIntColumn.Description,
                OrderIndex = smallIntColumn.OrderIndex,
                IsNotNull = smallIntColumn.IsNotNull,
                DefaultValue = smallIntColumn.DefaultValue,
                Maximum = smallIntColumn.Maximum,
                Minimum = smallIntColumn.Minimum
            };
        }
    }
}
